package br.com.comanda.webservice.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import br.com.comanda.webservice.dao.AccountDAO;
import br.com.comanda.webservice.model.Account;

public class AccountDAOImpl extends GenericDAOImpl<Account> implements
		AccountDAO {

	public AccountDAOImpl() {
		super(Account.class);
	}

	public void closeAccount(int accountId) {
		String hql = "update Account set close = true where id = :accountId";
		Query query = this.getSession().createQuery(hql);
		query.setParameter("accountId", accountId);

		this.getSession().beginTransaction().begin();
		query.executeUpdate();
		this.getSession().beginTransaction().commit();
	}

	public List<Account> findOpenAccounts() {
		Criteria criteria = this.getSession().createCriteria(Account.class);
		criteria.add(Restrictions.eq("close", false));
		return criteria.list();
	}

}
